﻿using System;
using System.Collections.Generic;
using System.Data;
using BLL;
using Utils;
namespace UI.ModSalesman
{
    public partial class ChangeAdd : System.Web.UI.Page
    {
        SalesmanBiz salesmanBiz = new SalesmanBiz();
        DefDicBiz defDicBiz = new DefDicBiz();
        EmployeeBiz employeeBiz = new EmployeeBiz();

        string account = "";


        protected void Page_Load(object sender, EventArgs e)
        {
            string changeID = "0";
            string saleManID = "0"; //通过人员列表界面，弹出界面来获得基本信息
            if (Session["user"] != null)
            {
                User user = Session["user"] as User;
                account = user.Account;
            }
            else
            {
                JSUtility.Alert(this, "您没有登录!请先登录。");
                return;
            }
            if (!IsPostBack)
            {
                if (Request.Params["SaleManID"] != null)
                {
                    saleManID = Request.Params["SaleManID"].ToString();
                }
                if (Request.Params["ChangeID"] != null)
                {
                    changeID = Request.Params["ChangeID"].ToString();
                    ViewState["VsChangeID"] = changeID;
                    btnAdd.Enabled = false;
                }
                else
                {
                    btnUpdate.Enabled = false;
                    btnDelete.Enabled = false;
                }

                JSUtility.AddConfirm(btnDelete, "您真的要删除该职位变动的信息吗？");
                txtChangeTime.Text = System.DateTime.Now.ToString("yyyy-MM-dd");
                if (changeID != "0") BindData(changeID);
                else if (saleManID != "0")
                {
                    DataTable dt = salesmanBiz.GetOneInfo(saleManID); ;
                    FillData(dt);
                }

            }
        }
        private void BindData(string changeID)
        {
            DataTable dt = salesmanBiz.GetChangeOneInfo(changeID);
            if (dt.Rows.Count > 0)
            {
                hidSaleManID.Value = dt.Rows[0]["SaleManID"].ToString();
                txtEmpCode.Text = dt.Rows[0]["EmpCode"].ToString();
                txtOldDeptName.Text = dt.Rows[0]["OldDeptName"].ToString();

                txtEmpName.Text = dt.Rows[0]["EmpName"].ToString();
                txtStateName.Text = dt.Rows[0]["StateName"].ToString();
                txtTypeName.Text = dt.Rows[0]["TypeName"].ToString();
                txtNewDeptName.Text = dt.Rows[0]["NewDeptName"].ToString();

                string ChangeTime = dt.Rows[0]["ChangeTime"].ToString();
                if (Utils.RegexRule.IsDate(ChangeTime))
                    txtChangeTime.Text = Convert.ToDateTime(ChangeTime).ToString("yyyy-MM-dd");

                txtMemo.Text = dt.Rows[0]["Memo"].ToString();
                dt.Dispose();
            }
            else
            {
                JSUtility.Alert(this, "无数据");
            }

        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {

            string empCode = txtEmpCode.Text.Trim();
            if (txtNewDeptName.Text == "" || empCode == "")
            {
                JSUtility.Alert(this, "请选择部门，工号不能为空");
                return;
            }
            if (!salesmanBiz.IsExistEmpCode(empCode, "在职"))
            {
                JSUtility.Alert(this, "不存在该售货员编号");
                return;
            }
            string changeTime = txtChangeTime.Text.Trim();
            if (!RegexRule.IsDate(changeTime))
            {
                JSUtility.Alert(this, "请输入正确的日期格式！");
                return;
            }
            string hidID = hidSaleManID.Value;
            if (string.IsNullOrEmpty(hidID))
            {
                DataTable idtb = salesmanBiz.GetOneInfo(empCode, "在职");
                if (idtb.Rows.Count > 0)
                {
                    hidID = idtb.Rows[0]["SaleManID"].ToString();
                }
                else
                {
                    JSUtility.Alert(this, "输入的工号不存在！");
                    return;
                }
            }
            SortedDictionary<string, string> sdList = new SortedDictionary<string, string>();
            sdList.Add("SaleManID", hidID);
            sdList.Add("EmpCode", empCode);
            sdList.Add("EmpName", txtEmpName.Text.Trim());
            sdList.Add("OldDeptName", txtOldDeptName.Text.Trim());
            sdList.Add("NewDeptName", txtNewDeptName.Text);
            sdList.Add("StateName", txtStateName.Text.Trim());
            sdList.Add("TypeName", txtTypeName.Text.Trim());
            sdList.Add("ChangeTime", changeTime);
            sdList.Add("Memo", txtMemo.Text.Trim());
            sdList.Add("Account", account);
            string info = "";
            if (salesmanBiz.Insert("Admin_Salesman_Change", sdList))
            {
                info = "增加信息成功";
                //更改促销员部门
                SortedDictionary<string, string> hList = new SortedDictionary<string, string>();
                hList.Add("SaleManID", hidID);
                hList.Add("DeptName", txtNewDeptName.Text);

                if (salesmanBiz.Update("Admin_Salesman", hList)) info += "\n更新信息成功";
                else info += "\n更新信息失败";
                JSUtility.AlertAndRedirect(info, "ChangeAdd.aspx");
            }
            else
                JSUtility.Alert(this, "增加信息失败");

        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            string changeID = ViewState["VsChangeID"].ToString();
            if (changeID == "0")
            {
                JSUtility.Alert(this, "调用错误");
                return;
            }
            string empCode = txtEmpCode.Text.Trim();
            if (txtNewDeptName.Text == "")
            {
                JSUtility.Alert(this, "请选择部门");
                return;
            }
            if (!salesmanBiz.IsExistEmpCode(empCode, "在职"))
            {
                JSUtility.Alert(this, "不存在该售货员编号");
                return;
            }
            string changeTime = txtChangeTime.Text.Trim();
            if (!RegexRule.IsDate(changeTime))
            {
                JSUtility.Alert(this, "请输入正确的日期格式！");
                return;
            }
            if (string.IsNullOrEmpty(hidSaleManID.Value))
            {
                JSUtility.Alert(this, "请从列表重新进入该界面，进行更新操作！");
                return;
            }
            SortedDictionary<string, string> sdList = new SortedDictionary<string, string>();
            sdList.Add("ChangeID", changeID);
            sdList.Add("SaleManID", hidSaleManID.Value);
            sdList.Add("EmpCode", empCode);
            sdList.Add("EmpName", txtEmpName.Text.Trim());
            sdList.Add("OldDeptName", txtOldDeptName.Text.Trim());
            sdList.Add("NewDeptName", txtNewDeptName.Text);
            sdList.Add("StateName", txtStateName.Text.Trim());
            sdList.Add("TypeName", txtTypeName.Text.Trim());
            sdList.Add("ChangeTime", changeTime);
            sdList.Add("Memo", txtMemo.Text.Trim());
            sdList.Add("Account", account);
            if (changeID != "0")
            {
                string info = "";
                if (salesmanBiz.Update("Admin_Salesman_Change", sdList))
                {
                    info = "成功更新信息";
                    //更改员工部门
                    SortedDictionary<string, string> hList = new SortedDictionary<string, string>();
                    hList.Add("SaleManID", hidSaleManID.Value);
                    hList.Add("DeptName", txtNewDeptName.Text.Trim());
                    if (salesmanBiz.Update("Admin_Salesman", hList)) info += "\n更新信息成功";
                    else info += "\n更新信息失败";
                    BindData(changeID);
                }
                else
                    info = "更新信息失败";
                JSUtility.Alert(this, info);
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            string changeID = ViewState["VsChangeID"].ToString();
           
            if (changeID != "0")
            {
                if (salesmanBiz.DeleteChangeOneInfo(changeID))
                    JSUtility.Alert(this, "删除成功");
                else
                    JSUtility.Alert(this, "删除失败");
            }
            else
                JSUtility.Alert(this, "请先选择记录");
        }



        protected void txtEmpCode_TextChanged(object sender, EventArgs e)
        {
            string empCode = txtEmpCode.Text.Trim();
            if (!string.IsNullOrWhiteSpace(empCode))
            {
                if (Utils.StringUtility.IncludeHZ(empCode))//如果包含汉字
                {
                    DataTable dt = salesmanBiz.GetOneInfoByName(empCode, "在职");
                    if (dt.Rows.Count == 0)
                    {
                        JSUtility.Alert(this, "不存在该员工姓名: " + empCode);
                    }
                    else if (dt.Rows.Count == 1)
                    {
                        FillData(dt);
                    }
                    else
                    {
                        string info = "存在同名，请输入工号：\n\n";
                        for (int i = 0; i < dt.Rows.Count; i++)
                        {
                            info += dt.Rows[i]["EmpCode"].ToString() + "  " + dt.Rows[i]["EmpName"].ToString() + "\n";
                        }
                        JSUtility.Alert(this, info);
                    }
                }
                else
                {
                    DataTable dt = salesmanBiz.GetOneInfo(empCode, "在职");
                    if (dt.Rows.Count > 0)
                    {
                        hidSaleManID.Value = dt.Rows[0]["SaleManID"].ToString();
                        txtOldDeptName.Text = dt.Rows[0]["DeptName"].ToString();
                        txtEmpName.Text = dt.Rows[0]["EmpName"].ToString();
                        txtStateName.Text = dt.Rows[0]["StateName"].ToString();
                        txtTypeName.Text = dt.Rows[0]["TypeName"].ToString();
                    }
                }
            }
        }
        protected void FillData(DataTable dt)
        {
            if (dt.Rows.Count == 1)
            {
                hidSaleManID.Value = dt.Rows[0]["SaleManID"].ToString();
                txtEmpCode.Text = dt.Rows[0]["EmpCode"].ToString();
                txtOldDeptName.Text = dt.Rows[0]["DeptName"].ToString();
                txtEmpName.Text = dt.Rows[0]["EmpName"].ToString();
                txtStateName.Text = dt.Rows[0]["StateName"].ToString();
                txtTypeName.Text = dt.Rows[0]["TypeName"].ToString();
            }
        }

        protected void btnClose_Click(object sender, EventArgs e)
        {
            JSUtility.CloseWindow();
        }


    }
}